package com.sky.mapper.user;

import com.sky.pojo.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AddressBookMapper {


    /**
     * 查询登录用户所有地址
     * @return
     */
    @Select("select * from  address_book")
    public List<AddressBook> list();

    /**
     * 新增地址
     * @param addressBook
     */
    void add(AddressBook addressBook);

    /**
     * 查询默认地址
     * @return
     */
    @Select("select * from address_book where is_default=1")
    AddressBook getDfault();



    /**
     * 根据id查询地址
     * @param id
     */
    @Select("select * from  address_book where id=#{id}")
    AddressBook getId(Integer id);



    /**
     * 修改地址
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     * @param id
     */
    @Delete("delete from address_book where id=#{id}")
    void deleteId(Integer id);


    /**
     * 设置默认地址
     * @param addressBook
     */
    @Update("UPDATE address_book SET is_default = CASE WHEN id = #{id} THEN 1 ELSE 0 END")
    void updateDefault(AddressBook addressBook);

}
